﻿using K9Nano.Dependency;

namespace K9Nano.Reporting;

public class ReportManager(IReportStore store) : IScopedDependency, IReportManager
{
    public virtual async Task<IEnumerable<ReportSummary>> GetReportSummariesAsync(CancellationToken cancellation)
    {
        return await store.GetAllReportsAsync(cancellation);
    }

    public virtual Task<Report?> GetAsync(int id, CancellationToken cancellation)
        => store.GetAsync(id, cancellation);

    public virtual Task SaveAsync(Report report, CancellationToken cancellation)
         => store.SaveAsync(report, cancellation);

    public virtual Task DeleteAsync(int id, CancellationToken cancellation)
         => store.DeleteAsync(id, cancellation);

    public virtual async Task<ReportForm?> GetReportFormAsync(int id, CancellationToken cancellation)
    {
        var report = await store.GetAsync(id, cancellation);
        if (report == null) return null;
        return new ReportForm(report);
    }

    public virtual Task<bool> SetStatusAsync(int id, bool isEnabled, CancellationToken cancellation)
        => store.SetStatusAsync(id, isEnabled, cancellation);
}

public record ReportSummary(int Id, string DisplayName, string Description);

public record ReportForm(int Id, string DisplayName, string Description,
    IEnumerable<ReportFormItem> FormItems, IEnumerable<ReportColumn> Columns)
{
    public ReportForm(Report report) : this(report.Id,
        report.DisplayName,
        report.Description,
        report.FormItems,
        report.Columns)
    {
    }
}
